<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>

        <el-button type="primary" size="mini" @click="addVisible=true" >添加分类</el-button>
      </div>
    <!-- 表格区域 -->
    <!-- :data要渲染的数据,必须是一个数组,prop:指定要渲染的属性名 -->
    <!-- border添加边框,stripe各行变色 -->
  <el-table style="width: 100%;" :data="cateList" border stripe>
  <el-table-column label="文章标题" type="index" width="100"></el-table-column>
  <el-table-column label="文章分类" prop="cate_name"></el-table-column>
  <el-table-column label="发表时间" prop="cate_alias"></el-table-column>
  <el-table-column label="状态">
   <template v-slot=" {row} ">
    <el-button type="primary" size="mini" @click="showChange(row) ">修改</el-button>
    <el-button type="danger" size="mini" @click="hDel(row.id) ">删除</el-button>
   </template>
  </el-table-column>
</el-table>
    </el-card>
     <!--  设置close-on-click-modal="false"不关闭dialog -->
<el-dialog
     title="添加文章分类"
     :visible.sync="addVisible"
      width="35%"
      :close-on-click-modal="false"
      @closed="$refs.addRef.resetFields() "
     >
    <!-- 添加的表单 -->
<el-form :model="addForm" :rules="addRules" ref="addRef" label-width="100px">
  <el-form-item label="分类名称" prop="cate_name">
    <el-input v-model="addForm.cate_name"  style="width:350pz"></el-input>
  </el-form-item>
  <el-form-item label="分类别名" prop="cate_alias">
    <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15" ></el-input>
  </el-form-item>

</el-form>
     <span slot="footer" class="dialog-footer">
    <el-button @click="addVisible = false" size="mini">取 消</el-button>
    <el-button type="primary" @click="hSubmit" size="mini">确 定</el-button>
  </span>
</el-dialog>

<el-dialog
     title="修改文章分类"
     :visible.sync="cgVisible"
      width="35%"
      :close-on-click-modal="false"
      @closed="$refs.cgRef.resetFields() "
     >
    <!-- 修改的表单 -->
     <el-form :model="cgForm" :rules="cgRules" ref="cgRef" label-width="100px">
     <el-form-item label="分类名称" prop="cate_name">
       <el-input v-model="cgForm.cate_name"  style="width:350pz"></el-input>
     </el-form-item>
     <el-form-item label="分类别名" prop="cate_alias">
       <el-input v-model="cgForm.cate_alias" minlength="1" maxlength="15" ></el-input>
     </el-form-item>
   </el-form>

     <span slot="footer" class="dialog-footer">
    <el-button @click="cgVisible = false" size="mini">取 消</el-button>
    <el-button type="primary" @click="hDate" size="mini">确定</el-button>
  </span>
</el-dialog>
  </div>

</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {

      cateList: [],
      addVisible: false,
      cgVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''

      },
      cgForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          },

          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ]

      },
      cgRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          },

          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ]

      }
    }
  },
  created () {
    this.initCateList()
  },
  methods: {
    async initCateList () {
      const { data: res } = await this.$http.get('/my/cate/list')

      if (res.code !== 0) return
      this.cateList = res.data
    },

    hSubmit () {
      // 表单预校验
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        // 发起请求
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error('添加分类失败！')
        this.$message.success('添加分类成功！')
        // 关闭对话框
        this.addVisible = false
        // 重新请求列表数据
        this.initCateList()
      })
    },
    showChange (row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('管理员不允许修改此项')
      this.cgForm = { ...row }
      this.cgVisible = true
    },
    hDate () {
      this.$refs.cgRef.validate(async valid => {
        if (!valid) return
        // 发起请求
        const { data: res } = await this.$http.put('/my/cate/info', this.cgForm)
        if (res.code !== 0) return this.$message.error('修改分类失败！')
        this.$message.success('修改分类成功！')
        // 关闭对话框
        this.cgVisible = false
        // 重新请求列表数据
        this.initCateList()
      })
    },
    hDel (id) {
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除这个分类')
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http({
          url: '/my/cate/del',
          method: 'delete',
          params: {
            id
          }
        })
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.initCateList()
      }).catch(() => {

      })
    }

  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
